﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace JasRibbonApplicatie.Model
{
    public class Model
    {

        // return true wanneer alles in orde is => werkt niet voordat entities.savechanges gedaan is....
        //public static bool ControleScancodeByAanvraag(int aanvraagId, String streepjescode)
        //{
        //    var controleQuery = from product in entities.ontleningen
        //                        where product.aanvraag_id.Equals(aanvraagId)
        //                        && product.streepjescode.Equals(streepjescode)
        //                        select product;
        //    if (controleQuery.ToList().Count == 0)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        // gaat gebruikt worden bij Ontleningen-views

        public static jasEntities entities = new jasEntities();

        public void OpslaanWijzigingen(bool deffered = true)
        {
            MainWindow mw = Login.Instance.MainWindowReference;
            mw.actieStatusBarItem.Content = "Wijzigingen Opslaan";
            mw.actieStatusBarItem.Visibility = System.Windows.Visibility.Visible;
            mw.vooruitgangStatusBarItem.Visibility = System.Windows.Visibility.Visible;
            mw.vooruitgangProgressBar.Value = 100;
            mw.ErrorStatusBarItem.Visibility = System.Windows.Visibility.Collapsed;
            if (deffered)
            {
                Task.Factory.StartNew(() =>
                {
                    //do work on background task
                    try
                    {
                        //entities.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
                        entities.AcceptAllChanges();
                    }
                    catch (Exception ex)
                    {

                        mw.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new DispatcherOperationCallback(delegate
                        {
                            mw.ErrorTextBlock.Text = ex.ToString();
                            mw.ErrorStatusBarItem.Visibility = System.Windows.Visibility.Visible;
                            return null;
                        }), null);
                    }
                    finally
                    {
                        mw.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new DispatcherOperationCallback(delegate
                        {
                            mw.vooruitgangStatusBarItem.Visibility = System.Windows.Visibility.Collapsed;
                            mw.actieStatusBarItem.Visibility = System.Windows.Visibility.Collapsed;
                            return null;
                        }), null);
                    }
                });
            }
            else
            {
                try
                {
                    //entities.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);// updateException bij niet unieke primary keys...
                    entities.AcceptAllChanges();
                }
                catch (Exception ex)
                {
                    mw.ErrorTextBlock.Text = ex.ToString();
                    mw.ErrorStatusBarItem.Visibility = System.Windows.Visibility.Visible;
                }
                finally
                {
                    mw.vooruitgangStatusBarItem.Visibility = System.Windows.Visibility.Collapsed;
                    mw.actieStatusBarItem.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }
    }
}
